package com.hmkj.core.api.alibaba;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.hmkj.boot.utils.SpringContextUtils;
import com.hmkj.core.api.ninepai.utils.MerchantConfig;
import com.hmkj.core.api.zhutong.util.HttpRequest;
import com.hmkj.core.api.zhutong.util.MD5Gen;
import com.hmkj.core.api.zhutong.util.TimeUtil;
import com.hmkj.core.api.zhutong.util.ZhuTongSmsUtil;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.service.cache.RedisService;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.Setter;

import java.net.URLEncoder;
import java.util.Map;
import java.util.function.Consumer;

public class AliyunSmsUtil {

    @Setter
    private String accessKeyId; /**阿里云accessKeyId**/

    @Setter
    private String accessKeySecret; /**阿里云accessKeySecret**/

    @Setter
    private boolean enable; /*短信接口是否启用*/

    private static AliyunSmsUtil instance;

    public AliyunSmsUtil(Consumer<AliyunSmsUtil> consumer) {
        consumer.accept(this);
    }

    public AliyunSmsUtil(String accessKeyId,String accessKeySecret, boolean enable) {
        this.accessKeyId = accessKeyId;
        this.accessKeySecret = accessKeySecret;
        this.enable = enable;
    }


    /**
     * @param param
     * @Description
     */
    public SendSmsResponse sendSmsByPost(Map<String,Object> param) throws ClientException {
        if (!enable) return null;
        String phone = (String)param.get("mobile");
        if (StrUtil.isBlank(phone)) throw new BussinessException("手机号不能为空");
        String template = (String)param.get("template");
        String code = (String)param.get("code");
        if (StrUtil.isBlank(phone)) throw new BussinessException("短信code不能为空");
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        String product = AliyunConstant.product;
        String domain = AliyunConstant.domain;
        String msgSign = AliyunConstant.msgSign;

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(msgSign);
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(template);
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam("{\"code\":"+code+"}");
        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        return sendSmsResponse;
    }

}

